package com.wx.mall.operate.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.CommonResult;
import com.wx.mall.common.api.ResultCode;
import com.wx.mall.common.exceptions.ApiAssert;
import com.wx.mall.operate.dto.RebDisDto;
import com.wx.mall.operate.dto.RebateDto;
import com.wx.mall.operate.dto.RebateSaveDto;
import com.wx.mall.operate.entity.RebBrand;
import com.wx.mall.operate.entity.RebDiscount;
import com.wx.mall.operate.entity.RebGoods;
import com.wx.mall.operate.entity.Rebate;
import com.wx.mall.operate.service.impl.RebateServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author j
 * @since 2020-06-01
 */
@RestController
@RequestMapping("/rebate")
public class RebateController {

    @Autowired
    RebateServiceImpl rebateService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/getPageFind")
        public CommonResult<CommonPage<Rebate>> getPageFinds(RebateDto dto){
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<Rebate> list=rebateService.findAllRebate(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "筛选查询")
    @GetMapping("/getFindScreen")
    public CommonResult<CommonPage<Rebate>> getFindScreenList(RebateDto dto){
        System.out.println(dto);
        if (dto.getPageNum() == null || dto.getPageSize() == null){
            ApiAssert.fail(ResultCode.VALIDATE_FAILED);
        }
        if (dto.getPageNum() == 0){
            ApiAssert.fail("无效页码");
        }
        List<Rebate> list=rebateService.findScreenList(dto);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "修改满减")
    @PostMapping("/getUpdateRebate")
    public CommonResult<Rebate> getUpdateRebateList(RebDisDto dto){
        int disList = rebateService.findDisList(dto);
        return disList==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "添加满减")
    @PostMapping("/getSaveRebate")
    public CommonResult<Rebate> getSaveRebate(String rebDiscounts,String rebate,String reLink,String aGoods,String aBrands){
        List<RebDiscount> rebDiscount = JSON.parseArray(rebDiscounts, RebDiscount.class);
        RebateSaveDto rebateSaveDto = JSON.parseObject(rebate, RebateSaveDto.class);
        List<Long> aLong = JSON.parseArray(reLink, Long.class);
        List<RebGoods> rebGoods = JSON.parseArray(aGoods, RebGoods.class);
        List<RebBrand> rebBrands = JSON.parseArray(aBrands, RebBrand.class);
        int i = rebateService.saveRebate(rebDiscount,rebateSaveDto,aLong,rebGoods,rebBrands);
        return i==1?CommonResult.success():CommonResult.failed();
    }

    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/getDeleteRebate")
    public CommonResult<Rebate> getDeleteRebate(Long ids){
        System.out.println(ids);
        int i = rebateService.deleteRebate(ids);
        return i==1?CommonResult.success():CommonResult.failed();
    }
}

